local md__kuzhan = fk.CreateSkill {
  name = "md__kuzhan",
  tags = { Skill.Quest , Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["md__kuzhan"] = "苦战",
  [":md__kuzhan"] = "使命技，<br>\
  ⬤　成功：准备阶段，若你本局累计受到过至少5点伤害，你获得技能“退敌”。<br>\
  ⬤　失败：结束阶段，若你本回合和上回合均发动过“借兵”且未造成过伤害。",

  ["#md__kuzhan-invoke"] = "清玉：你需弃置两张手牌，防止你受到的伤害",
  ["@kuzhan"] = "受伤累计",
  ["@md__weizaochengshanghai"] = "未伤害累计",
  ["@md__jiebing_fadongguo"] = "借兵累计",
  ["$md__kuzhan1"] = "大家之韵，不可失之。",
  ["$md__kuzhan2"] = "朱沉玉没，桂殒兰凋。",
  ["$md__kuzhan3"] = "冰清玉粹，岂可有污！",
}

md__kuzhan:addEffect(fk.Damaged, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(md__kuzhan.name, true)
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:addPlayerMark(player, "@kuzhan", data.damage)
  end,
})


md__kuzhan:addEffect(fk.EventPhaseStart, {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(md__kuzhan.name) and
      player.phase == Player.Start and player:getMark("@kuzhan") >= 5
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player.room:setPlayerMark(player, "@kuzhan", 0)
    -- player:broadcastSkillInvoke(md__kuzhan.name, 1)
    room:notifySkillInvoked(player, md__kuzhan.name, "special")
    room:handleAddLoseSkills(player, "md__tuidi")
    room:updateQuestSkillState(player, md__kuzhan.name, false)
    room:invalidateSkill(player, md__kuzhan.name)
  end,
})

md__kuzhan:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    return player == target and player:hasSkill(md__kuzhan.name) and player.phase == Player.Finish -- and
    
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if(#player.room.logic:getActualDamageEvents(1, function(e) return e.data.from == player end) == 0) then
      if(player:getMark("@md__weizaochengshanghai") < 2) then
        room:addPlayerMark(player, "@md__weizaochengshanghai", 1)
      end
    else
      if(player:getMark("@md__weizaochengshanghai") > 0) then
        -- player:removeMark("@md__weizaochengshanghai", 1)
        room:setPlayerMark(player, "@md__weizaochengshanghai", 0)
      end
    end
    if(player:usedSkillTimes("md__jiebing") ~= 0) then
      if(player:getMark("@md__jiebing_fadongguo") < 2) then
        room:addPlayerMark(player, "@md__jiebing_fadongguo", 1)
      end
    else
      if(player:getMark("@md__jiebing_fadongguo") > 0) then
        -- player:removeMark("@md__jiebing_fadongguo", 1)
        room:setPlayerMark(player, "@md__jiebing_fadongguo", 0)
      end
    end

  end,
})

-- md__kuzhan:addEffect(fk.EventPhaseStart, {
--   can_trigger = function(self, event, target, player, data)
--     return player == target and player:hasSkill(md__kuzhan.name) and player.phase == Player.Finish and
--     #player.room.logic:getActualDamageEvents(1, function(e) return e.data.from == player end) ~= 0
--   end,
--   on_use = function(self, event, target, player, data)
--     local room = player.room
--     if(player:getMark("@md__weizaochengshanghai") > 0) then
--       -- player:removeMark("@md__weizaochengshanghai", 1)
--       room:setPlayerMark(player, "@md__weizaochengshanghai", 0)
--     end
--   end,
-- })

-- md__kuzhan:addEffect(fk.EventPhaseStart, {
--   can_trigger = function(self, event, target, player, data)
--     return player == target and player:hasSkill(md__kuzhan.name) and player.phase == Player.Finish and
--     player:usedSkillTimes("md__jiebing") ~= 0
--   end,
--   on_use = function(self, event, target, player, data)
--     local room = player.room
--     if(player:getMark("@md__jiebing_fadongguo") < 2) then
--       room:addPlayerMark(player, "@md__jiebing_fadongguo", 1)
--     end
--   end,
-- })

-- md__kuzhan:addEffect(fk.EventPhaseStart, {
--   can_trigger = function(self, event, target, player, data)
--     return player == target and player:hasSkill(md__kuzhan.name) and player.phase == Player.Finish and
--     player:usedSkillTimes("md__jiebing") == 0
--   end,
--   on_use = function(self, event, target, player, data)
--     local room = player.room
--     if(player:getMark("@md__jiebing_fadongguo") > 0) then
--       -- player:removeMark("@md__jiebing_fadongguo", 1)
--       room:setPlayerMark(player, "@md__jiebing_fadongguo", 0)
--     end
--   end,
-- })

md__kuzhan:addEffect(fk.EventPhaseEnd, {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(md__kuzhan.name) and player.phase == Player.Finish and player:getMark("@md__jiebing_fadongguo") >= 2
    and player:getMark("@md__weizaochengshanghai") >= 2
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    -- player:broadcastSkillInvoke(md__kuzhan.name, 2)
    room:notifySkillInvoked(player, md__kuzhan.name, "negative")
    room:updateQuestSkillState(player, md__kuzhan.name, true)
    room:invalidateSkill(player, md__kuzhan.name)
  end,
})

return md__kuzhan
